package com.smart.four.ioc.context;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

/**
 * 测试ApplicationContext
 *
 * @author junrui.wang
 * @create 2018/4/27
 */
public class ApplicationContextT {
    public static void text(){
        //类路径加载applicationContext文件,对于ClassPathXmlApplicationContext，com/smart/ioc/factorybean/beans.xml 等于 classpath:com/smart/ioc/factorybean/beans.xml
        ApplicationContext applicationContext1 = new ClassPathXmlApplicationContext("com/smart/ioc/context/beans.xml");
        Car car1 = applicationContext1.getBean("Car", Car.class);
        car1.introduce();

        ApplicationContext applicationContext2 = new ClassPathXmlApplicationContext("classpath:com/smart/ioc/context/beans.xml");
        Car car2 = applicationContext2.getBean("Car", Car.class);
        car2.introduce();

        //文件路径加载（配置文件放在文件系统的路径下，可以优先考虑FileSystemXmlApplicationContext） 对于ClassPathXmlApplicationContext，com/smart/ioc/context/beans.xml 等于 file:com/smart/ioc/context/beans.xml
        ApplicationContext applicationContext3 = new FileSystemXmlApplicationContext("com/smart/ioc/context/beans.xml");
        Car car3 = applicationContext3.getBean("Car", Car.class);
        car3.introduce();

        ApplicationContext applicationContext4 = new FileSystemXmlApplicationContext("file:com/smart/ioc/context/beans.xml");
        Car car4 = applicationContext4.getBean("Car", Car.class);
        car4.introduce();

        //可以指定多个配置文件，Spring会自动将多个配置文件整合成一个
        ApplicationContext applicationContext5 = new ClassPathXmlApplicationContext(new String[]{"com/smart/ioc/context/beans.xml","com/smart/ioc/context/beans1.xml"});
        Car car5 = applicationContext5.getBean("Car", Car.class);
        car5.introduce();
        Car car6 = applicationContext5.getBean("Car2", Car.class);
        car6.introduce();


    }
}
